package com.kk.search.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kk.clients.ProductClient;
import com.kk.doc.ProductDoc;
import com.kk.pojo.Product;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * projectName: b2c-store
 *
 * @author: kjy
 * time: 2023/5/6 19:06
 * description:监控boot程序启动，完成es数据的同步工作
 */
@Slf4j
@Component
public class SpringBootListener implements ApplicationRunner {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private ProductClient productClient;

    private String indexStr = "";
    /**
     * 需要在此方法，完成es数据的同步！
     *  1.判断es中的product索引是否存在
     *  2.不存在，利用java代码创建一个
     *  3.存在，删除原来的数据
     *  4.查询商品全部数据
     *  5.进行es库的更新工作[插入]
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {

        //1.判断es中是否存在product索引
        GetIndexRequest getIndexRequest = new GetIndexRequest("product");
        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);

        //2.判断处理
        if (exists) {
            //存在 删除原有数据
            DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest("product");
            deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery());//全部删除的含义
            restHighLevelClient.deleteByQuery(deleteByQueryRequest,RequestOptions.DEFAULT);
        }else {
            //不存在，创建新的索引表即可，product
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("product");
            createIndexRequest.source(indexStr, XContentType.JSON);
            restHighLevelClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);
        }


        //3.查询全部商品数据
        List<Product> productList = productClient.allList();

        //4.批量数据插入
        BulkRequest request = new BulkRequest();

        //json转换对象
        ObjectMapper objectMapper = new ObjectMapper();

        for (Product product : productList) {
            ProductDoc productDoc = new ProductDoc(product);
            //用于插入数据的作用
            IndexRequest indexRequest = new IndexRequest("product").id(product.getProductId().toString());
            //productDoc转成json放入
            String json = objectMapper.writeValueAsString(productDoc);
            indexRequest.source(json,XContentType.JSON);
            request.add(indexRequest);
        }


        restHighLevelClient.bulk(request,RequestOptions.DEFAULT);
    }
}
